<html>
<head>
	<title> System Management &amp; Security </title>
	<link rel="stylesheet" type="text/css" href="css/html.css">
	<link rel="stylesheet" type="text/css" href="css/docbook.css">
	<link rel="stylesheet" type="text/css" href="css/syntax.css">
</head>


<body>
<h1> 7 The Shell </h1>

<div class="TOC">
<dl>
	<dt><b>Table of Contents</b></dt>
	<dt>7.1 <a href="chapter7.html#SHELL">    The Shell            </a></dt>
	<dt>7.2 <a href="chapter7.html#HISTORY">  FreeBSD's sh History </a></dt>
	<dt>7.3 <a href="chapter7.html#OVERVIEW"> Overview of sh       </a></dt>
	<dt>7.4 <a href="chapter7.html#EXECCOM">  Executing commands   </a></dt>
	<dt>7.5 <a href="chapter7.html#PARSER">   Lexical Structure    </a></dt>
	<dt>7.6 <a href="chapter7.html#EXPANSION">Expansion            </a></dt>
	<dt>7.7 <a href="chapter7.html#PATHEXP">  Pathname Expansion   </a></dt>	
</dl>
</div>

<!------------------ 7.1 The Shell ---------------------------------------->
<div class="SECT1">
<h1 class="SECT1"><a id="SHELL" name="SHELL">7.1 The Shell</a></h1>
<p>
A Unix shell is a command-line interpreter and script host that provides a traditional user 
interface for the Unix operating system and for Unix-like systems. Users direct the operation 
of the computer by entering command input as text for a command line  interpreter to execute 
or by creating text scripts of one or more such commands. The shell creates processes and
waits for them to terminate if the command is not followed by &amp;.
The most generic sense of the term shell means any program that users use to type commands. 
Since in the Unix operating system users can select which shell they want to use (which 
program should execute when they log in), many shells have been developed. It is called a "shell"
because it hides the details of the underlying operating system behind the shell's
interface (in contrast with the "kernel", which refers to the lowest-level, or 'inner-most' 
component of an operating system). In Unix, any program can be the user's shell. Users who 
want to use a different syntax for typing commands can specify a different program as their 
shell, though in practice this usually requires administrator rights.
The term shell also refers to a particular program, such as the Bourne shell, sh. The Bourne 
shell was the shell used in early versions of Unix and became a de facto standard. Every 
Unix-like system has at least one shell compatible with the Bourne shell (bash, ksh, ash, zsh,..).
The Bourne shell program is located in the Unix file hierarchy at  <tt class="FILENAME">/bin/sh</tt>.
There are also different shells such as csh, tcsh,... .
</p>
<small>
Reference <a href="http://en.wikipedia.org/wiki/Unix_shell">Wikipedia: Unix Shell</a>
</small>
</div>


<!------------------ 7.2 FreeBSD's sh History ---------------------------------------->
<div class="SECT2">
<h1 class="SECT2"><a id="HISTORY" name="HISTORY">7.2 FreeBSD's sh History</a></h1>

<p>
A sh command, the Thompson shell, appeared in Version 1 AT&amp;T UNIX.  It
was superseded in Version 7 AT&amp;T UNIX by the Bourne shell, which inher-
ited the name sh.<br>

This version of sh was rewritten in 1989 under the BSD license after the
Bourne shell from AT&amp;T System V Release 4 UNIX.
</p>
<small>
Reference <a href="http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=FreeBSD+7.1-RELEASE&format=html">sh Manual Page</a>
</small>
</div>


<!------------------ 7.3 Overview of sh ---------------------------------------->
<div class="SECT3">
<h1 class="SECT3"><a id="OVERVIEW" name="OVERVIEW">7.3 Overview of sh</a></h1>
<p>
The shell is a command that reads lines from either a file or the terminal, 
interprets them, and generally executes other commands.  It is the
program that is started when a user logs into the system, although a user
can select a different shell with the <a href="http://www.freebsd.org/cgi/man.cgi?query=chsh&sektion=1&apropos=0&manpath=FreeBSD+7.1-RELEASE">chsh(1)</a>a command.  The shell implements 
a language that has flow control constructs, a macro facility that
provides a variety of features in addition to data storage, along with
built-in history and line editing capabilities.  It incorporates many
features to aid interactive use and has the advantage that the interpretative 
language is common to both interactive and non-interactive use
(shell scripts).  That is, commands can be typed directly to the running
shell or can be put into a file, which can be executed directly by the
shell.
</p>
<small>
Reference <a href="http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=FreeBSD+7.1-RELEASE&format=html">sh Manual Page</a>
</small>
</div>

<!------------------ 7.4 Executing Commands ---------------------------------------->
<div class="SECT4">
<h1 class="SECT4"><a id="EXECCOM" name="EXECCOM">7.4 Executing commands</a></h1>

<p>
When locating a command, the shell first looks to see if it has a shell
function by that name. Then it looks for a built-in command by that
name. If a built-in command is not found, one of two things happen:
</p>
<p>
1. Command names containing a slash are simply executed without performing any searches.
</p>
<p>
2. The shell searches each entry in the PATH environment variable in
turn for the command.  The value of the PATH variable should be a
series of entries separated by colons.  Each entry consists of a
directory name.  The current directory may be indicated implicitly
by an empty directory name, or explicitly by a single period.
</p>

<small>
Reference <a href="http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=FreeBSD+7.1-RELEASE&format=html">sh Manual Page</a>
</small>
</div>


<!------------------ 7.5 Lexical Structure ---------------------------------------->
<div class="SECT5">
<h1 class="SECT5"><a id="PARSER" name="PARSER">7.5 Lexical Structure</a></h1>

<p>
The shell reads input in terms of lines from a file and breaks it up into
words at whitespace (blanks and tabs), and at certain sequences of char-
acters called operators, which are special to the shell.  

There are two types of operators: control operators and redirection operators.  
</p>

<p>Control operators:</p>
<pre>
&amp;  &amp;&amp;  (  )  \n  ;;  ;  |  ||
</pre>

<p>Redirection operators:</p>
<pre>
&lt;   &gt;	  &lt;&lt;  &gt;&gt;  &lt;&gt;  &lt;&amp;  &gt;&amp;  &lt;&lt;-  &gt;|
</pre>

<p>
The operators lose their special meaning by quoting them. Quoting is used to remove 
the special meaning of certain characters or words to the shell, such as operators, 
whitespace, keywords, or alias names. There are three types of quoting:
</p>

<p><b>Single Quotes(')</b>:
Enclosing characters in single quotes preserves the literal meaning of all the characters
(except single quotes, making it impossible to put single-quotes in a single-quoted string).
</p>
<p><b>Double Quotes(")</b>:
Enclosing characters within double quotes preserves the literal
meaning of all characters except dollar sign ($), backquote
(`), and backslash (\). 
</p>

<p><b>Backslash(\)</b>:
A backslash preserves the literal meaning of the following chaeacter, with 
the exception of the newline character (\n). A backslash preceding a newline 
is treated as a line continuation.
</p>

<small>
Reference <a href="http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=FreeBSD+7.1-RELEASE&format=html">sh Manual Page</a>
</small>
</div>


<!------------------ 7.6 Expansion ---------------------------------------->
<div class="SECT6">
<h1 class="SECT6"><a id="EXPANSION" name="EXPANSION">7.6 Expansion</a></h1>

<p>
Some expressions are substituted by other strings. The order of the substitution is
important.
</p>

<dl>
<dt>1. brace expansion ({})                       </dt>
<dt>2. tilde expansion (~)                        </dt>
<dt>3. variable/parameter expansion ($)           </dt>
<dt>4. command substitution ('cmd' or $(cmd))     </dt>
<dt>5. arithmetic expansion ($((expression)))     </dt>
<dt>6. word splitting (meta + control characters) </dt>
<dt>7. pathname expansion (* ? [])                </dt>
<dt>8. quote removal ("...", '...', \)            </dt>
</dl>


<div class="CODELISTING">
<pre>
$ ls -l file{1,2}
-rw-r--r-- 1 bfriedrich bfriedrich 0 2009-03-29 22:27 file1
-rw-r--r-- 1 bfriedrich bfriedrich 0 2009-03-29 22:27 file2

$ echo ~root
/root

$ echo ~{root,bfriedrich}
/root /home/bfriedrich

$ echo $USER
bfriedrich

$ echo today is 'date'
today is date

$ echo today is `date`
today is So 29. Mär 22:32:02 CEST 2009

$ echo $((3+9*5))
48

$ ls -l file?
-rw-r--r-- 1 bfriedrich bfriedrich 0 2009-03-29 22:27 file1
-rw-r--r-- 1 bfriedrich bfriedrich 0 2009-03-29 22:27 file2

$ ls -l *2
-rw-r--r-- 1 bfriedrich bfriedrich 0 2009-03-29 22:27 file2
</pre>
</div>

</div>

<!------------------ 7.7 Pathname Expansion ---------------------------------------->
<div class="SECT7">
<h1 class="SECT7"><a id="PATHEXP" name="PATHEXP">7.7 Pathname Expansion</a></h1>
<p>
An asterisk (<b>*</b>) matches any string of characters.  A question mark
(`?') matches any single character.  A left bracket (<b>[</b>) introduces a
character class.  The end of the character class is indicated by a <b>]</b>;
if the <b>]</b> is missing then the <b>[</b> matches a <b>[</b> rather than introducing
a character class.  A character class matches any of the characters
between the square brackets.  A range of characters may be specified
using a minus sign.  The character class may be complemented by making an
exclamation point (<b>!</b>) the first character of the character class.
</p>
<small>
Reference <a href="http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=FreeBSD+7.1-RELEASE&format=html">sh Manual Page</a>
</small>
</div>


</body>
</html>